"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:ProjectDeductCtrl
 * @description
 * # ProjectDeductCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller("ProjectDeductCtrl", function (
    $scope,
    $rootScope,
    $location,
    openApi,
    Util,
    $routeParams,
    FormSubmit,
    localStorageService
  ) {
    var id = ($scope.process_id = $routeParams.id);

    // 选择扣款方式
    $scope.costTypeList = [
      {
        key: "保证金",
        value: 3
      },
      {
        key: "押金",
        value: 4
      },
      {
        key: "缴纳",
        value: 5
      },
      {
        key: "成本",
        value: 6
      },
      {
        key: "收入",
        value: 7
      }
    ];
    // 选择款项用途
    $scope.getcostTypeObj = function () {
      var param = {};
      param.type = "项目款项小类";
      param.parent = $scope.costTypeList.filter(function (item) {
        return item.value == $scope.param.receiveType;
      })[0].key;
      Util.getKeyValue(param, function (data) {
        $scope.receiveSubTypeList = data.data;
      });
    };

    $scope.chooseProject = function () {
      // 将列表的数据序列化存入缓存以便回来时候使用
      Util.savaParamForReview($scope.param, id);
      $location.path("project/chooseproject");
    };

    $scope.costTypechange = function () {
      $scope.picker.deductName = "";
      $scope.getcostsubTypeList();
    };
    $scope.getcostsubTypeList = function () {
      var param = {};
      param.type = "项目款项小类";
      param.parent = $scope.costTypeList.filter(function (item) {
        return item.value == $scope.picker.deductType;
      })[0].key;
      Util.getKeyValue(param, function (data) {
        $scope.costsubTypeList = data.data;
      });
    };
    $scope.showCertificate = function () {
      $scope.showCerPicker = true;
      $rootScope.$emit("showmasker");
    };
    $scope.deletedItem = function (index) {
      $scope.param.details.splice(index, 1);
      $scope.costindex = -1;
    };

    $scope.addCertificate = function () {
      $scope.picker = $scope.picker || {};
      var check = false;
      check = checkPicker($scope.picker);
      if (!check) {
        return;
      }
      $scope.param.details.push($scope.picker);
      // 隐藏表单
      closePicker();
    };
    var closePicker = function () {
      $scope.picker = {};
      $rootScope.$emit("closemasker");
      $scope.showCerPicker = false;
    };
    $scope.$on("closemasker", function () {
      closePicker();
    });
    $scope.closePicker = function () {
      closePicker();
    };
    $scope.swipe = function (index, type) {
      if (type) {
        $scope.payindex = index;
      } else {
        $scope.costindex = index;
      }
    };
    $scope.resetlist = function (type) {
      if (type) {
        $scope.payindex = -1;
      } else {
        $scope.costindex = -1;
      }
    };
    // 验证是否输入完整
    var checkPicker = function (data) {
      if (!data.deductAmount) {
        Util.Toast("请填写扣款金额");
        return;
      }
      if (!data.deductType) {
        Util.Toast("请选择款项类型");
        return;
      }
      if (!data.deductRemark) {
        Util.Toast("请填写扣款事由");
        return;
      }
      return true;
    };
    $scope.submit = function (operationId) {
      $scope.param.deducts = angular.toJson($scope.param.details);
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.projectCost;
      options.param = angular.copy($scope.param, {});
      delete options.param.details;
      FormSubmit.submit(options, $scope);
    };
    $scope.validate = function () {
      if (!$scope.param.projectName) {
        Util.Toast("请选择项目名称");
        return;
      }
      if (
        $scope.param.relatedModuleName &&
        $scope.param.relatedModuleName !== "无"
      ) {
        if (!$scope.param.moduleNo) {
          Util.Toast("请选择模块编号");
          return;
        }
      }
      if (!$scope.param.closeOutType) {
        Util.Toast("请选择扣款方式");
        return;
      }
      if ($scope.param.details.length < 1) {
        Util.Toast("请填写扣款明细");
        return;
      }
      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    };

    function getList() {
      var param = {};
      param.type = "项目扣款类型";
      Util.getKeyValue(param, function (data) {
        $scope.deductTypeList = data.data;
      });
    }

    var init = function () {
      // 获取回显的详细信息
      Util.getDetailData(id, function (data) {
        $scope.param = data;
        $scope.param.details = $scope.param.details || [];
        var data = localStorageService.get("projectinfo");
        localStorageService.remove("projectinfo");
        $scope.param = Object.assign({}, $scope.param, data);
        if($scope.param.closeOutType){
          $scope.param.closeOutType=""+$scope.param.closeOutType
        }
        getList()
      });
    };
    // 初始化
    init();
  });
